package com.yao.controller;

import com.yao.entity.ServerEntity;
import com.yao.entity.UserEntity;
import com.yao.service.ServerService;
import com.yao.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ServerController {

    @Autowired
    private ServerService serverService;
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;
    private final static Logger logger = LoggerFactory.getLogger(ServerController.class);

    //跳转到login页面
    @GetMapping(value = "/login")
    public String login() {
        logger.info("------跳转到登录页面------");
        return "login";
    }

    //登录
    @PostMapping(value = "/login")
    @ResponseBody
    public Map<String, String> login(
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "password", required = true) String password,
            HttpSession session) {
        Map<String, String> ret = new HashMap<String, String>();
        if (StringUtils.isEmpty(username)) {
            ret.put("type", "error");
            ret.put("msg", "用户名不能为空!");
            logger.info("------用户名为空------");
            return ret;
        }
        if (StringUtils.isEmpty(password)) {
            ret.put("type", "error");
            ret.put("msg", "密码不能为空!");
            logger.info("------密码为空------");
            return ret;
        }
        UserEntity userEntity = userService.findByUserName(username);
        if (userEntity == null) {
            ret.put("type", "error");
            ret.put("msg", "用户不存在!");
            logger.info("------用户不存在------");
            return ret;
        }
        if (!password.equals(userEntity.getPassword())) {
            ret.put("type", "error");
            ret.put("msg", "密码错误!");
            logger.info("------密码错误------");
            return ret;
        }
        session.setAttribute("username", username);
        ret.put("type", "success");
        ret.put("msg", "登录成功!");
        logger.info("------登陆成功------");
        return ret;
    }

    //注销
    @GetMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        logger.info("------注销------");
        return "login";
    }
    //跳转页面
    @GetMapping("list")
    public String car(){
        return "car";
    }
    @GetMapping("center")
    public String center(){
        return "center";
    }
    @GetMapping("guozhen")
    public String guozhen(){
        return "guozhen";
    }
    @GetMapping("headquarters")
    public String headquarters(){
        return "headquarters";
    }

    //获取表格数据
    @RequestMapping(value = "/lists")
    @ResponseBody
    public Map<String,Object> lists(@RequestParam("server") String server,
                                   Integer page,
                                   Integer limit) {
        List<ServerEntity> servers = serverService.findAll(server);
        int pageNum=(page-1)*limit;
        List<ServerEntity> serv = serverService.findPage(server,pageNum,limit);
        Map<String, Object> tableData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        tableData.put("code", 0);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", servers.size());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", serv);
        //返回给前端
        return tableData;
    }

    //多条件模糊查询
    @PostMapping("/select")
    @ResponseBody
    public Map<String, Object> select(
                         @RequestParam(value = "server") String server,
                         @RequestParam(value = "cabinet",required = false) String cabinet,
                         @RequestParam(value = "types",required = false) String types,
                         @RequestParam(value = "stat",required = false) String stat,
                         @RequestParam(value = "ip",required = false) String ip,
                         @RequestParam(value = "app",required = false) String app,
                         @RequestParam(value = "systems",required = false) String systems,
                         @RequestParam(value = "apptype",required = false) String apptype,
                         @RequestParam(value = "datas",required = false) String datas,
                         @RequestParam(value = "numbers",required = false) String numbers,
                         @RequestParam(value = "brand",required = false) String brand,
                         @RequestParam(value = "sn",required = false) String sn,
                         @RequestParam(value = "service",required = false) String service,
                         @RequestParam(value = "startDate",required = false) Date startDate,
                         @RequestParam(value = "endDate",required = false) Date endDate,
                         @RequestParam(value = "cpu",required = false) String cpu,
                         @RequestParam(value = "amount",required = false) String amount,
                         @RequestParam(value = "core",required = false) String core,
                         @RequestParam(value = "ram",required = false) String ram,
                         @RequestParam(value = "disk",required = false) String disk,
                         @RequestParam(value = "capacity",required = false) String capacity,
                         @RequestParam(value = "remain",required = false) String remain,
                         @RequestParam(value = "used",required = false) String used,
                         @RequestParam(value = "admin",required = false) String admin,
                         @RequestParam(value = "department",required = false) String department,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit) {
        Map<String, Object> tableData = new HashMap<String, Object>();
        List<ServerEntity> servers = serverService.findBySomething(server,cabinet,types,stat,ip,app,systems,
                apptype,datas,numbers,brand,sn,service,startDate,endDate,cpu,amount,
                core,ram,disk,capacity,remain,used,admin,department);
        int pageNum = (page - 1) * limit;
        List<ServerEntity> serv = serverService.findByPage(server,cabinet,types,stat,ip,app,systems,
                apptype,datas,numbers,brand,sn,service,startDate,endDate,cpu,amount,
                core,ram,disk,capacity,remain,used,admin,department,pageNum,limit);
        tableData.put("code", 0);
        tableData.put("msg", "查询成功");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", servers.size());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", serv);
        //返回给前端
        return tableData;
    }

    // 删除一条信息
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam("server") String server,@RequestBody int[] ids) {
        Map<String, Object> tableData = new HashMap<String, Object>();
        for (int id : ids) {
            serverService.delete(server,id);
        }
        tableData.put("code", 200);
        tableData.put("msg", "删除成功");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", 0);
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", "");
        //返回给前端
        return tableData;
    }

    //编辑信息
    @PostMapping("/edit")
    @ResponseBody
    public Map<String, Object> edit(@RequestParam("server") String server,
                       @RequestParam("edit_id") int id,
                       @RequestParam("edit_cabinet") String cabinet,
                       @RequestParam("edit_positions") String positions,
                       @RequestParam("edit_types") String types,
                       @RequestParam("edit_stat") String stat,
                       @RequestParam("edit_ip") String ip,
                       @RequestParam("edit_app") String app,
                       @RequestParam("edit_systems") String systems,
                       @RequestParam("edit_apptype") String apptype,
                       @RequestParam("edit_datas") String datas,
                       @RequestParam("edit_numbers") String numbers,
                       @RequestParam("edit_brand") String brand,
                       @RequestParam("edit_sn") String sn,
                       @RequestParam("edit_service") String service,
                       @RequestParam("edit_dates") Date dates,
                       @RequestParam("edit_cpu") String cpu,
                       @RequestParam("edit_amount") String amount,
                       @RequestParam("edit_core") String core,
                       @RequestParam("edit_ram") String ram,
                       @RequestParam("edit_disk") String disk,
                       @RequestParam("edit_capacity") String capacity,
                       @RequestParam("edit_remain") String remain,
                       @RequestParam("edit_used") String used,
                       @RequestParam("edit_admin") String admin,
                       @RequestParam("edit_department") String department,
                       @RequestParam("edit_remark") String remark) {
        Map<String, Object> tableData = new HashMap<String, Object>();
            serverService.update(server,id,cabinet,positions,types,stat,ip,app,systems,
                    apptype,datas,numbers,brand,sn,service,dates,cpu,amount,
                    core,ram,disk,capacity,remain,used,admin,department, remark);
        tableData.put("code", 200);
        tableData.put("msg", "编辑成功");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", 0);
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", "");
        //返回给前端
        return tableData;
    }

    // 添加信息
    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> add(
                        @RequestParam("server") String server,
                         @RequestParam("add_cabinet") String cabinet,
                         @RequestParam("add_positions") String positions,
                         @RequestParam("add_types") String types,
                         @RequestParam("add_stat") String stat,
                         @RequestParam("add_ip") String ip,
                         @RequestParam("add_app") String app,
                         @RequestParam("add_systems") String systems,
                         @RequestParam("add_apptype") String apptype,
                         @RequestParam("add_datas") String datas,
                         @RequestParam("add_numbers") String numbers,
                         @RequestParam("add_brand") String brand,
                         @RequestParam("add_sn") String sn,
                         @RequestParam("add_service") String service,
                         @RequestParam("add_dates") Date dates,
                         @RequestParam("add_cpu") String cpu,
                         @RequestParam("add_amount") String amount,
                         @RequestParam("add_core") String core,
                         @RequestParam("add_ram") String ram,
                         @RequestParam("add_disk") String disk,
                         @RequestParam("add_capacity") String capacity,
                         @RequestParam("add_remain") String remain,
                         @RequestParam("add_used") String used,
                         @RequestParam("add_admin") String admin,
                         @RequestParam("add_department") String department,
                         @RequestParam("add_remark") String remark) {
        Map<String, Object> tableData = new HashMap<String, Object>();
        serverService.add(server,cabinet,positions,types,stat,ip,app,systems,
                apptype,datas,numbers,brand,sn,service,dates,cpu,amount,
                core,ram,disk,capacity,remain,used,admin,department, remark);
        tableData.put("code", 200);
        tableData.put("msg", "添加成功！");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", 0);
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", "");
        //返回给前端
        return tableData;
    }

}
